package com.addcn.addcnwebview.webview;

import android.content.Context;
import android.text.TextUtils;
import android.webkit.WebResourceResponse;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * author:LuWenbo
 * date: On 2018/12/18
 * content:
 * describe:
 */
public class WebViewDataHelper {

    private Map<String, String> mMap;

    public WebViewDataHelper() {
        mMap = new HashMap<>();
        initData();
    }

    private void initData() {
//        mMap.put("https://c.8891.com.tw/assets/js/common/jquery-1.7.2.min.js","js/jquery-1.7.2.min.js");
//        mMap.put("https://c.8891.com.tw/assets/css/article/touch.css?t=20190816","css/touch.css");
//        mMap.put("graphics/gyro2.js","js/gyro2.js");
//        mMap.put("gio.js","js/gio.js");
//        mMap.put("graphics/KolorBox/KolorBox.min.js","js/KolorBox.min.js");
//        mMap.put("graphics/KolorMenu/KolorMenu.min.js","js/KolorMenu.min.js");
//        mMap.put("pano/h5_index.css","css/pano_h5_index.css");
//        mMap.put("pano/common.css","css/pano_common.css");
//        mMap.put("pano/index.css","css/pano_index.css");
//        mMap.put("graphics/KolorBox/kolorBox.css","css/kolorBox.css");
    }
    public boolean hasLocalResource(String url){
        if (url.contains("xvdata/")&&!url.endsWith(".jpg")) {
            return mMap.containsKey(url.split("xvdata/")[1]);
        }else if (url.contains("css/pano/common.css")){
            return mMap.containsKey("css/pano/common.css");
        }else if (url.contains("gio.js")){
            return mMap.containsKey("gio.js");
        }else if (url.contains("pano/h5_index.css")||url.contains("pano/index.css")){
            return true;
        }else{
            return mMap.containsKey(url);
        }
    }

    public WebResourceResponse getReplacedWebResourceResponse(Context context, String url){
        String localResourcePath;
        if (url.contains("xvdata/")&&!url.endsWith(".jpg")) {
            localResourcePath = mMap.get(url.split("xvdata/")[1]);
        }else if (url.contains("gio.js")) {
            localResourcePath = mMap.get("gio.js");
        }else if (url.contains("pano/common.css")) {
            localResourcePath = mMap.get("pano/common.css");
        }else if (url.contains("pano/h5_index.css")){
            localResourcePath = mMap.get("pano/h5_index.css");
        }else if (url.contains("pano/index.css")){
            localResourcePath = mMap.get("pano/index.css");
        }else{
            localResourcePath = mMap.get(url);
        }
        if (TextUtils.isEmpty(localResourcePath)) {
            return null;
        }
        if (TextUtils.isEmpty(localResourcePath)) {
            return null;
        }
        InputStream is = null;
        try{
            is=context.getApplicationContext().getAssets().open(localResourcePath);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        String mimeType;
        if (url.contains("css")) {
            mimeType = "text/css";
        } else if (url.contains("jpg")) {
            mimeType = "image/jpeg";
        }else {
            mimeType = "image/png";
        }
        WebResourceResponse response = new WebResourceResponse(mimeType, "utf-8", is);
        return response;
    }

}
